package com.zxl.yjb.fragment.home.order.take;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.home.order.CommentActivity;
import com.zxl.yjb.activity.home.order.OrderDetailActivity;
import com.zxl.yjb.activity.home.user.UserSpaceActivity;
import com.zxl.yjb.adapter.OrderTakeAdapter;
import com.zxl.yjb.base.LazyFragment;
import com.zxl.yjb.bean.OrderTakeBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public abstract class OrderTakeBaseFragment extends LazyFragment {
    @BindView(R.id.rv_order)
    RecyclerView rv_order;
    @BindView(R.id.sm_order)
    SmartRefreshLayout sm_order;

    private DataManager dataManager;

    private int page = 1;
    private int count = 10;
    private int order_status = 0;
    private int pay_status = 0;
    private String state = "false";


    abstract int setOrderStatus();
    abstract int setPayStatus();
    abstract String setState();

    private RecyclerView.LayoutManager layoutManager;
    private ArrayList<OrderTakeBean> takeBeanList = new ArrayList<>();
    private OrderTakeAdapter orderTakeAdapter;

    @Override
    protected void lazyLoad() {

    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        getData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order;
    }
    @Override

    protected void initView(View view, Bundle savedInstanceState) {
        order_status = setOrderStatus();
        pay_status = setPayStatus();
        state = setState();

        layoutManager = new LinearLayoutManager(getActivity());
        rv_order.setLayoutManager(layoutManager);
        orderTakeAdapter = new OrderTakeAdapter(R.layout.item_order_send,takeBeanList,order_status,pay_status);
        rv_order.setAdapter(orderTakeAdapter);

        orderTakeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                takeBeanList.get(position).setHaveread(1);
                orderTakeAdapter.notifyDataSetChanged();
                Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
                intent.putExtra("id",takeBeanList.get(position).getOrder_sn());
                int request_id = takeBeanList.get(position).getRequest_id();
                intent.putExtra("user_id", request_id)
                        .putExtra("order_status", order_status)
                        .putExtra("pay_status", pay_status)
                        .putExtra("from", "take_list");
                startActivity(intent);
            }
        });
        orderTakeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OrderTakeBean orderTakeBean = (OrderTakeBean) adapter.getItem(position);
                if (view.getId() == R.id.btn_comment) {
                    startActivity(new Intent(getContext(), CommentActivity.class)
                            .putExtra("friend_id", orderTakeBean.getRequest_id() + "")
                            .putExtra("friend_img", orderTakeBean.getFace_img())
                            .putExtra("title", orderTakeBean.getCat_type_name() + "_" + orderTakeBean.getServer_name())
                            .putExtra("time", orderTakeBean.getEnd_time())
                            .putExtra("order_sn", orderTakeBean.getOrder_sn())
                           .putExtra("from", "take_list"));//这里全部时接单人对发单人的评论


                }
                else if (view.getId() == R.id.iv_head_img)
                {
                    Intent intent = new Intent(getContext(), UserSpaceActivity.class);
                    intent.putExtra("uid",orderTakeBean.getRequest_id()+"");
                    startActivity(intent);
                }
            }
        });

        sm_order.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                refreshLayout.setNoMoreData(false);
                refreshLayout.finishRefresh(5000);
            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                refreshLayout.finishRefresh(5000);
            }
        });


    }

    @Override
    protected void main() {

    }


    private void getData()
    {
        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        map.put("count",count+"");
        map.put("order_status",order_status+"");
        map.put("pay_status",pay_status+"");
        map.put("state",state);
        ObserverOnNextListener<ArrayList<OrderTakeBean>> listener = new ObserverOnNextListener<ArrayList<OrderTakeBean>>() {
            @Override
            public void onNext(ArrayList<OrderTakeBean> orderSendBeans) {
                if (page == 1)
                    takeBeanList.clear();
                takeBeanList.addAll(orderSendBeans);
                orderTakeAdapter.notifyDataSetChanged();
                if (orderSendBeans.size() < count) {
                    sm_order.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                }
                if (sm_order!=null)
                {
                    sm_order.finishRefresh();
                    sm_order.finishLoadMore();
                }

            }
        };
        dataManager.yjlists(new ProgressObserver<ArrayList<OrderTakeBean>>(listener,getActivity()),map);
    }

}
